package demo4;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-08-08
 * Time: 15:35
 */
class Main {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要扫描的根目录（绝对路径 OR 相对路径): ");
        String rootDirPath = scanner.next();
        File rootDir = new File(rootDirPath);
        if (!rootDir.isDirectory()) {
            System.out.println("您输入的根目录不存在或者不是目录，退出");
            return;
        }
        System.out.print("请输入要找出的文件名中的字符: ");
        String token = scanner.next();
        List<File> result = new ArrayList<>();
        // 因为文件系统是树形结构，所以我们使用深度优先遍历（递归）完成遍历
        scanDir(rootDir, token, result);
        System.out.println("共找到了符合条件的文件 " + result.size() + "个，它们分别是");
        for (File file : result) {
            System.out.println(file.getCanonicalPath() + " 请问您是否要删除该文件？y/n");
            String in = scanner.next();
            if (in.toLowerCase().equals("y")) {
                file.delete();
            }
        }
    }
    private static void scanDir(File rootDir, String token, List<File> result) {
        File[] files = rootDir.listFiles();
        if (files == null || files.length == 0) {
            return;
        }
        for (File file : files) {
            if (file.isDirectory()) {
                scanDir(file, token, result);
            } else {
                if (file.getName().contains(token)) {
                    result.add(file.getAbsoluteFile());
                }
            }
        }
    }
}
public class TestDemo3 {

    public static void main(String[] args) {
        //输入你要删除的文件目录
        System.out.println("输入你要删除的文件目录");
        Scanner scan = new Scanner(System.in);
        String root = scan.next();
        File rootPath = new File(root);
        if(!rootPath.isDirectory()){
            //输入的不是目录
            System.out.println("输入的不是目录!!!,即将退出程序");
            return;
        }
        System.out.println("请输入你要删除的文件~~~");
        Scanner scan1 = new Scanner(System.in);
        String toDelete = scan1.next();
        List<File> list = new ArrayList<>();
        scanDir(rootPath,list,toDelete);
        System.out.println("与其类似的文件共找到" + list.size()+"个");
        //遍历list看是否要删除
        for(File file : list){
            try {
                System.out.println("是否要删除Y/N" + file.getCanonicalPath());
                Scanner sc = new Scanner(System.in);
                String s = sc.next();
                if(s.toLowerCase().equals("y")){
                    file.delete();
                    System.out.println("删除成功");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    public static void scanDir(File rootPath,List<File> result,String toDelete){
        File[] flies = rootPath.listFiles();
        if(flies==null||flies.length==0){
            try {
                System.out.println("文件递归路径如下" + rootPath.getCanonicalPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        for(File f : flies){
            if(f.isDirectory()){
                scanDir(f,result,toDelete);
            }else {
                result.add(f);
            }
        }
    }
}
